package com.shensi.appCollect.service.impl;

import com.shensi.appCollect.dao.SysUserMapper;
import com.shensi.appCollect.pojo.SysUser;
import com.shensi.appCollect.service.SysUserService;
import com.shensi.appCollect.utils.redis.RedisCacheUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private RedisCacheUtil redisCacheUtil;

    //根据手机号获取用户信息
    @Override
    public SysUser selectSysUser(SysUser sysUser) {
        return sysUserMapper.selectSysUser(sysUser);
    }

    @Override
    public List<SysUser> selectSysUserList(SysUser sysUser) {
        return sysUserMapper.selectSysUserList(sysUser);
    }
    @Override
    public List<SysUser> selectSendUser(SysUser sysUser){
        return sysUserMapper.selectSendUser(sysUser);
    }




    //    @Override
//    public List<SysUser> Authentication(SysUser sysUser) {
//        SysUserExample example = new SysUserExample();
//        example.or().andUserTypeEqualTo(sysUser.getUserType());
//        if(sysUser.getUserType().equals('1')){
//            //公众号
//
//        }else if(sysUser.getUserType().equals('2')){
//            //小程序
//        }
//        return sysUserMapper.selectByExample(example);
//    }


    public List<SysUser> selectSysUserListForPush(SysUser sysUser) {
        if(sysUser.getStationId()== null){
            return new ArrayList<SysUser>();
        }
        String key = "push:emplist:"+sysUser.getStationId();
        if(redisCacheUtil.hasKey(key)){
            return redisCacheUtil.getCacheList(key);
        }

        List<SysUser> sysUserList = sysUserMapper.selectSysUserListForPush(sysUser);
        redisCacheUtil.setCacheList(key, sysUserList);
        redisCacheUtil.expire(key,10, TimeUnit.HOURS);
        return sysUserList;
    }

    @Override
    public SysUser selectByPrimaryKey(Long userId) {
        return sysUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public boolean resetPwd(SysUser user) {
        return sysUserMapper.resetPwd(user);
    }

    @Override
    public List<SysUser> selectUserRoleByUserId(Long userId) {
        return sysUserMapper.selectUserRoleByUserId(userId);
    }
}
